﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DataAccess;
using Entity;

namespace StudentAttendanceChecking.Forms
{
    public partial class frmViewAttendance : Form
    {
        public frmViewAttendance()
        {
            InitializeComponent();
        }
        DataSet ds;
        DataTable _tbl= new DataTable();
        dsatt.spViewStudentDataTable baocao;
        public void fillComboboxClass()
        {
            using (SqlConnection conn = ConnectData.getConnection())
            {
                try
                {
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter("Select * from Class where ClassStatus=1", conn);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    cboClass.DataSource = ds.Tables[0];
                    cboClass.DisplayMember = "ClassName";
                    cboClass.ValueMember = "ClassID";
                    conn.Close();

                }
                catch (Exception e)
                {

                }

            }
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            fillComboboxClass();
        }

        private void cboClass_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cboClass_SelectedValueChanged(object sender, EventArgs e)
        {
            SqlConnection conn = ConnectData.getConnection();
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select Subjects.SubjectName,Subjects.SubjectCode from Subjects inner join Attendants on Attendants.SubjectCode= Subjects.SubjectCode inner join Class on Attendants.ClassID= Class.ClassID	where Class.ClassName like " + "'" + cboClass.Text + "'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cboSubject.DataSource = ds.Tables[0];
            cboSubject.DisplayMember = "SubjectName";
            cboSubject.ValueMember = "SubjectCode";
            conn.Close();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {

                SqlConnection conn = ConnectData.getConnection();
                conn.Open();
                SqlCommand cmd = new SqlCommand("spViewStudent", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@ClassID", SqlDbType.Int);
                Classs class1 = new Classs();

                class1.ClassID = Convert.ToInt32(cboClass.SelectedValue);
                cmd.Parameters["@ClassID"].Value = class1.ClassID;
                Subjects sub = new Subjects();

                sub.SubjectCode = cboSubject.SelectedValue.ToString();
                cmd.Parameters.Add("@SubjectCode", SqlDbType.VarChar);
                cmd.Parameters["@SubjectCode"].Value = sub.SubjectCode;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                ds = new DataSet();
                _tbl.Clear();
                //da.Fill(ds);
                da.Fill(_tbl);
                grvStudent.DataSource = _tbl;
            }catch(Exception){
                MessageBox.Show("Can not found Subject");
                _tbl.Clear();
            }
           
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            try
            {
                Classs class1 = new Classs();
                class1.ClassID = Convert.ToInt32(cboClass.SelectedValue);
                Subjects sub = new Subjects();

                sub.SubjectCode = cboSubject.SelectedValue.ToString();
                rptViewReport document = new rptViewReport();
                baocao = new dsattTableAdapters.spViewStudentTableAdapter().GetData(class1.ClassID, sub.SubjectCode);
                document.SetDataSource((DataTable)baocao);
                for (int i = 0; i < baocao.Columns.Count; i++)
                {
                    document.DataDefinition.FormulaFields[i].Text = string.Format("{{spViewStudent.{0}}}", baocao.Columns[i].ColumnName);
                    ((CrystalDecisions.CrystalReports.Engine.TextObject)document.ReportDefinition.ReportObjects[string.Format("Text{0}", i + 1)]).Text = baocao.Columns[i].ColumnName;
                }
                crystalReportViewer1.ReportSource = document;
            }
            catch (Exception)
            {
                MessageBox.Show("Can not found Subject");
                crystalReportViewer1.ReportSource = null; 
            }

        }
    }
}
